package com.zshan.clinic.cms.handler;


import com.zshan.clinic.cms.common.core.domain.AjaxResult;
import com.zshan.clinic.cms.common.exception.user.UserPasswordNotMatchException;
import com.zshan.clinic.common.exception.BusinessFailException;
import com.zshan.clinic.common.exception.SystemFailException;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;


/**
 * controller  异常统一处理
 */
@ControllerAdvice
@CommonsLog
public class ExceptionHandler {

    @org.springframework.web.bind.annotation.ExceptionHandler(SystemFailException.class)
    @ResponseBody
    public AjaxResult handleSystemFailException(HttpServletRequest request, SystemFailException ex) {
        log.error(ex.getMessage(), ex);
        return AjaxResult.error(ex.getMessage());
    }

    @org.springframework.web.bind.annotation.ExceptionHandler(BusinessFailException.class)
    @ResponseBody
    public AjaxResult handleBusinessFailException(HttpServletRequest request, BusinessFailException ex) {
        log.error(ex.getMessage(), ex);
        return AjaxResult.error(ex.getCode(),ex.getMessage());
    }


    /**
     * 超过最大文件限制
     * @param request
     * @param ex
     * @return
     */
    @org.springframework.web.bind.annotation.ExceptionHandler(MaxUploadSizeExceededException.class)
    @ResponseBody
    public AjaxResult handleMaxUploadSizeExceededException(HttpServletRequest request, MaxUploadSizeExceededException ex) {
        log.error(ex.getMessage(), ex);
        return AjaxResult.error("超过文件最大限制");
    }

    /**
     * json解析失败
     * @param request
     * @param ex
     * @return
     */
    @org.springframework.web.bind.annotation.ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public AjaxResult handleHttpMessageNotReadableException(HttpServletRequest request, HttpMessageNotReadableException ex) {
        log.error(ex.getMessage(), ex);
        return AjaxResult.error("参数格式错误");
    }

    /**
     * 超过最大文件限制
     * @param request
     * @param ex
     * @return
     */
    @org.springframework.web.bind.annotation.ExceptionHandler(UserPasswordNotMatchException.class)
    @ResponseBody
    public AjaxResult handleUserPasswordNotMatchException(HttpServletRequest request, UserPasswordNotMatchException ex) {
        log.error(ex.getMessage(), ex);
        return AjaxResult.error("用户不存在/密码错误");
    }



    @org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
    @ResponseBody
    public AjaxResult handleException(HttpServletRequest request, Exception ex) {
        String errorInfo = "CMS服务出现异常,请求地址:"+request.getRequestURI()+"，异常信息:"+ex.getMessage();
        log.error(errorInfo,ex);
        /**
         * 异步发送邮件
         */
        StringWriter stringWriter = new StringWriter();
        ex.printStackTrace(new PrintWriter(stringWriter));
        return AjaxResult.error("网络异常");
    }


}
